var client = require("../DAOImpl/mongodb");
var dataConfig = require("../config/database.json");
var host = dataConfig.host;
var database = dataConfig.db;
var db = new client(host, database);
var status = require("../config/status.json");

/*
 * @req.query: origin,destination,sensor(optional)
 */
function getDistance(req, res, next) {
	var google = require('../private/googleService/service');
	google.getDistance(req.query, function(err, data){
		if (!err) {
			var result = {
				'resultCode' : status.SUCCESS.VALUE,
				'distance' : data.routes[0].legs[0].distance,
				'duration' : data.routes[0].legs[0].duration
			};
			res.send(result);
		} else {
			var result = {
				'resultCode' : status.SYSTEMERROR.VALUE
			};
			res.send(result);
		}
	});
	return next;
}


/*
 * @req.body: cus_id {'cus_id': '1'}
 */
function waiting(req, res, next){
	var result = new Object();
	var selector = new Object();
	var document = new Object();
	selector.cus_id = req.body.cus_id;
	document.status = status.ORDERSTATUS.WAITING.VALUE;
	
	db.update(dataConfig.col_order, selector, document, function(err1, err2, doc_order){
		if(err1 || err2){
			result.status = status.ERROR.VALUE;
			result.err = err1 + ' ' + err2;
			res.send(result);
		}else{
			db.find(dataConfig.col_driver, {}, {fields:{_id:1}},function(err1, err2, doc_dri){
				if(err1 || err2){
					console.log(err1 + err2);
					result.status = status.SYSTEMERROR.VALUE;
					result.err = err1 + ' ' + err2;
					res.send(result);
				}else{
					var message = new Object();
					message.order = new Object();
					message.order.id = doc_order._id;
					message.order.status = status.ORDERSTATUS.WAITING.VALUE;
					var notifi = new notification(doc_dri, message, function(err, req, res, data){
						if(err){
							console.log(err);
							result.status = status.SYSTEMERROR.VALUE;
							result.err = err;
							res.send(result);
						}else{
							console.log('notification is done!');
							result.status = status.SUCCESS.VALUE;
							res.send(result);
						}
					});
				}
			});
		}
	});
	return next;
}

/*
 * @path /calRequest @req.body: cus_id {{'cus_id': '1'}}
 */

function cancelRequest(req, res, next){
	var result = new Object();
	var selector = new Object();
	var document = new Object();
	
	selector.cus_id = req.body.cus_id;
	selecotr.stauts = {$ne:status.ORDERSTATUS.CUSTOMERACCEPT.VALUE};
	document.status = status.ORDERSTATUS.CANCEL.VALUE;
	
	db.update(dadaConfig.col_order, selector, document, function(err3, err4, doc){
		if(err3 || err4){
			result.status = status.SYSTEMERROR.VALUE;
			res.send(result);
		}else{
			db.find(dataConfig.col_driver, {}, {fields:{_id:1}},function(err1, err2, doc_dri){
				if(err1 || err2){
					console.log(err1 + err2);
					result.status = status.SYSTEMERROR.VALUE;
					res.send(result);
				}else{
					var message = new Object();
					message.order = new Object();
					message.order.id = doc._id;
					message.order.status =  status.ORDERSTATUS.CANCEL.VALUE;
					var notifi = new notification(doc_dri, message, function(err, req, res, data){
						if(err){
							console.log(err);
							result.status = status.SYSTEMERROR.VALUE;
							result.err = err;
							res.send(result);
						}else{
							console.log('notification is done!');
							result.status = status.SUCCESS.VALUE;
							res.send(result);
						}
					});
				}
			});
		}
	});
	
	return next;
}
/*
 * @req.body: cus_id {'cus_id': '1'}
 */

function showCustomer(req, res, next){
	var result=new Object();
	var query={
			"_id":req.body.cus_id
	}
	db.findOne(database.col_customer,query,function(err1,err2,doc){
	// console.log(doc);
		if(err1||err2){
			result.status=status.ERROR.VALUE;
			result.err = err1 + ' ' + err2;
			console.log(err1 + ' ' + err2);
		}else{
			result.status=status.SUCCESS.VALUE;
			result.cus_info = new Object();
			result.cus_info.id=doc._id;
			result.cus_info.phone=doc.phone;
			result.cus_info.credit=doc.credit;
			result.cus_info.photo=doc.photo;
			console.log(doc);
			res.send(result);
		}	
	});
	return next;
}

/*
 * @path /sendAudio @req.body: cus_id dri_id encodedString
 */
function sendAudio(req, res, next){
	var result = new Object();
	
	result.resultCode = status.SUCCESS.VALUE;
	result.encodedString = req.body.encodedString;
	console.log(result);
	res.send(result);
	return next;
}
exports.services = [ getDistance, waiting, cancelRequest, showCustomer, sendAudio];
